<?
    final class Principio_Model_AccountMapper extends Principio_Mapper
    {
        protected static $db_table = null;
        protected static function getDbTableName() { return "Account"; }


        public static function delete(Principio_Model_Account $model_account)
        {
            try
            {
                self::getDBTable()->getAdapter()->beginTransaction();

                Principio_Model_AccountLinkMapper::reset($model_account);
                self::getDBTable()->delete(['id=?' => $model_account->getID()]);

                self::getDBTable()->getAdapter()->commit();

                return true;
            }
            catch (Exception $ex)
            {
                self::reportException($ex);
                self::getDBTable()->getAdapter()->rollback();

                return false;
            };
        }


        public static function checkUniqueLogin(Principio_Model_Account $model_account)
        {
            try
            {
                $select = self::getDBTable()->select(Zend_Db_Table_Abstract::SELECT_WITH_FROM_PART)->where("login=?", $model_account->getLogin())->where("company_id=?", $model_account->getModelCompany()->getID())->where("id<>?", $model_account->getID())->columns("id");
                return (is_null(self::getDBTable()->fetchRow($select)));
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        public static function save(Principio_Model_Account $model_account)
        {
            try
            {
                $data = [
                    'login' => $model_account->getLogin(),
                    'password' => $model_account->getPassword(),
                    'short_name' => $model_account->getShortName(),
                    'display_name' => $model_account->getDisplayName()
                ];

                if ($model_account->getID() == -1)
                {
                    $data['company_id'] = $model_account->getModelCompany()->getID();
                    $model_account->setID(self::getDBTable()->insert($data));
                }
                else
                    self::getDBTable()->update($data, ['id=?' => $model_account->getID()]);

                return true;
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        public static function fetch($id)
        {
            try
            {
                $account_raw = self::getDBTable()->find(intval($id));
                return (is_null($account_raw) ? false : new Principio_Model_Account($account_raw[0]->toArray()));
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        public static function fetchInCompany(Principio_Model_Company_Abstract $model_company_abstract)
        {
            if ($model_company_abstract->getID() == -1)
                return Principio_Model_Account::makeCollection();

            try
            {
                $accounts = self::getDBTable()->fetchAll(['company_id=?' => $model_company_abstract->getID()]);
                return Principio_Model_Account::makeCollection($accounts->toArray());
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        /*
        public static function search($string)
        {
            $search_fields = array(
                "login" => "Login",
                "display_name" => "Display name",
                "short_name" => "Short name"
            );

            $result = parent::searchInDB(self::getDBTable(), $search_fields, $string);

            foreach ($result as $res)
            {
                $model_search = new Principio_Model_Search($string, "Accounts");
                $menu_data = array("menu_name" => "accounts", "level" => EXPAND_ACCOUNTS, "company_id" => $res["company_id"], "open_level" => 2);
                $model_search->setId($res["id"])->setResultName($res["login"])->setOpenItemFunctionName("Accounts")->setParentID($res["company_id"])->setMenuData($menu_data);
                foreach ($res as $col_name => $value)
                {
                    if (strstr($value, $string) && in_array($col_name, array_keys($search_fields)))
                       $searchable_text[$search_fields[$col_name]] = $value;
                }
                $model_search->setResult($searchable_text);
                $search[$res["id"]] = $model_search;
            }
            return $search;
        }
        */
    }
?>